package com.hucais.etl.common.service;

import com.hucais.core.constant.Constants;
import com.hucais.core.utils.CommonUtils;
import com.hucais.etl.common.bean.CandidateCategory;
import com.hucais.etl.common.bean.DDCategory;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JavaCommonService {

    /**
     * 归类-书籍数据
     *
     * @param inputCategory         需要分类的书籍分类字段数据
     * @param candidateCategoryList 候选书籍分类列表
     * @return
     */
    public static String categorizeBookData(String inputCategory, List<CandidateCategory> candidateCategoryList) {
        if (Constants.NONMAL_STR_VAL.equals(inputCategory)) {
            return Constants.NORMAL_CATEGORY;
        } else {
            for (CandidateCategory category : candidateCategoryList) {
                List<String> list = category.getKeywordList();
                for (String s : list) {
                    if (inputCategory.contains(s)) {
                        return category.getCategoryName();
                    }
                }
            }
            return Constants.NORMAL_CATEGORY;
        }
    }

    /**
     * 归类-当当书籍数据
     *
     * @param inputCategory
     * @return
     */
    public static DDCategory categorizeDdBook(String inputCategory, Map<String, DDCategory> secondCategoryToKeyMap) {
        DDCategory d = new DDCategory();
        d.setFirstCategory(Constants.DD_NORMAL_CATEGORY);
        d.setSecondCategory(Constants.DD_NORMAL_CATEGORY);
        d.setThirdCategory(Constants.DD_NORMAL_CATEGORY);
        d.setFourthCategory(StringUtils.EMPTY);

        if (CommonUtils.isNotBlankExt(inputCategory)) {
            String dataSplit = "-";
            String[] arr = inputCategory.split(dataSplit);
            int arrLen = arr.length;
            if (arrLen >= 3) {
                String secondCategory = arr[1];
                if (secondCategoryToKeyMap.containsKey(secondCategory)) {
                    d.setFirstCategory(secondCategoryToKeyMap.get(secondCategory).getFirstCategory());
                    d.setSecondCategory(secondCategory);

                    if (arrLen == 3) {
                        d.setThirdCategory(arr[arrLen - 1]);
                        d.setFourthCategory(StringUtils.EMPTY);
                    } else if (arrLen == 4) {
                        d.setThirdCategory(arr[arrLen - 2]);
                        d.setFourthCategory(arr[arrLen - 1]);
                    } else {
                        StringBuilder thirdCategory = new StringBuilder();

                        for (int i = 2; i < arr.length - 1; i++) {
                            if (i == 2) {
                                thirdCategory.append(arr[i]);
                            } else {
                                thirdCategory.append("-").append(arr[i]);
                            }
                        }
                        d.setThirdCategory(thirdCategory.toString());
                        d.setFourthCategory(arr[arrLen - 1]);
                    }
                }
            }
        }
        return d;
    }

    /**
     * 归类-当当历史书籍数据
     *
     * @param inputCategory 需要分类的书籍分类字段数据
     * @param categoryMap   历史书籍分类映射
     * @return
     */
    public static DDCategory categorizeDdHistoryBook(String inputCategory, Map<String, DDCategory> categoryMap) {
        DDCategory d;
        if (CommonUtils.isBlankExt(inputCategory)) {
            d = new DDCategory();
            d.setHistoryCategory(inputCategory);
            d.setFirstCategory(Constants.DD_NORMAL_CATEGORY);
            d.setSecondCategory(Constants.DD_NORMAL_CATEGORY);
            d.setThirdCategory(Constants.DD_NORMAL_CATEGORY);
            d.setFourthCategory(StringUtils.EMPTY);
        } else {
            if (categoryMap.containsKey(inputCategory)) {
                d = categoryMap.get(inputCategory);
            } else {
                d = new DDCategory();
                d.setHistoryCategory(inputCategory);
                d.setFirstCategory(Constants.DD_NORMAL_CATEGORY);
                d.setSecondCategory(Constants.DD_NORMAL_CATEGORY);
                d.setSecondCategory(Constants.DD_NORMAL_CATEGORY);
                d.setThirdCategory(Constants.DD_NORMAL_CATEGORY);
                d.setFourthCategory(StringUtils.EMPTY);
            }
        }
        return d;
    }

    /**
     * 初始化历史-各分类权重映射
     *
     * @param categoryMap 分类详情Map
     * @return
     */
    public static Map<String, DDCategory> initHistoryCategoryWeightMap(Map<String, DDCategory> categoryMap) {
        Map<String, DDCategory> map = new HashMap<>();

        for (DDCategory d : categoryMap.values()) {
            String firstCategory = d.getFirstCategory();
            String secondCategory = d.getSecondCategory();
            String key = firstCategory + "_" + secondCategory;
            if (!map.containsKey(key)) {
                map.put(key, d);
            }
        }
        return map;
    }

}
